﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AUV5.Web.Pages
{
    public partial class ServiceVideo : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    if (Request["serviceID"] != null)
                    {
                        initService(Request["serviceID"].ToString());
                    }
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        private void initService(string serviceID)
        {
            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceID);
            hlShowService.NavigateUrl = "/Pages/ServiceDetail.aspx?serviceID=" + service.ServiceID;
            title.Text = service.Title;
            
            //用于生成视频上传的路径
            hfadddate.Value = service.CreateDate.ToString("yyyy-MM-dd");
            Model.ServiceAttachment attach=BLL.ServiceAttachmentBLL.Instance.GetVideo(serviceID);
            if (attach==null)
            {
                btnAddVideo.Visible = true;
                panelUploader.Visible = true;
                //没有视频则删除按钮不可见
                btnDelVideo.Visible = false;
                lbVideoStatus.Text = "尚未添加视频！";
            }
            else 
            {
                //视频已存在则添加按钮不可见
                btnDelVideo.Visible = true;
                btnAddVideo.Visible = false;
                panelUploader.Visible = false;
                if (attach.State == 0)
                {
                    imgVideo.ImageUrl = attach.ImagePath;
                    lbVideoStatus.Text = "视频未通过审核！";
                    lbVideoLength.Text = "视频长度为：" + attach.VideoLength;
                }
                else
                {
                    imgVideo.ImageUrl = attach.ImagePath;
                    lbVideoStatus.Text = "视频已通过审核！";
                    lbVideoLength.Text = "视频长度为：" + attach.VideoLength;
                }
            }
        }

        //添加视频
        protected void addVideo_Click(object sender, EventArgs e)
        {
            if (Request["serviceID"] != null)
            {
                if (System.IO.File.Exists(Server.MapPath(hfpath.Value)))
                {
                    string serviceID = Request["serviceID"].ToString();
                    BLL.ServiceAttachmentBLL.Instance.AddVideo(serviceID, hfpath.Value);
                    initService(serviceID);
                }
                else
                {
                    base.Alert("视频路径不正确！");
                }
            }
        }

        //移除视频
        protected void delVideo_Click(object sender, EventArgs e)
        {
            
        }

        //通过视频的审核，使其能显示到服务信息中
        //审核后无法删除源视频，因为视频地址没有记录，而且还包括上传后未提交的视频，所以最好通过其它方式进行删除
        //protected void passVideo_Click(object sender, EventArgs e)
        //{
        //    if (Request["serviceID"] != null)
        //    {
        //        string serviceID = Request["serviceID"].ToString();
        //        Model.ServiceAttachment att = BLL.ServiceAttachmentBLL.Instance.GetVideo(serviceID);
        //        if (att != null && att.State == 0)
        //        {
        //            att.State = 1;
        //            BLL.ServiceAttachmentBLL.Instance.Update(att);
        //        }
        //        initService(serviceID);
        //    }
        //}
    }
}